﻿using mediaPrime.Common;
using mediaPrime.Logger;
using mediaPrime.Sdk;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace mediaPrime.Core
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Returns current version number
        /// </summary>
        public static Version Version
        {
            get
            {
                return Assembly.GetEntryAssembly().GetName().Version;
            }
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            /*System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            */

            // Handle unhandled exceptions
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            // Initialize directories
            System.IO.Directory.CreateDirectory(Pathes.LogDir);

            // Load Settings
            Settings.LoadUserSettings(Pathes.UserSettings);
            Settings.LoadDefaultSettings(Pathes.DefaultSettings, "mediaPrime");

            Settings.Apps["mediaPrime"].RemoveOldSettings();
            Settings.Save(); // Save UserSettings after removing old settings

            // Initialize Logger
            var traceLevel = TraceLevel.Error;
            var logLevel = Settings.Apps["mediaPrime"].UserSettings["LogLevel"].GetValue<SettingEnumValue>();
            switch (logLevel.Value)
            {
                case "Off": traceLevel = TraceLevel.Off; break;
                case "Debug": traceLevel = TraceLevel.Verbose; break;
                case "Info": traceLevel = TraceLevel.Info; break;
                case "Warning": traceLevel = TraceLevel.Warning; break;
                case "Error": traceLevel = TraceLevel.Error; break;
            }

            Log.Initialize(Pathes.LogFile, traceLevel);

            base.OnStartup(e);
        }

        #region Exception Handling
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception)
                Log.Error("An unhandled exception occured: " + (e.ExceptionObject as Exception).ToString());
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (e.Exception != null)
                Log.Error("An unhandled exception occured: " + e.Exception.ToString());
        }


        #endregion
    }
}
